package com.fzsf.drawingboard.utils;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * <pre>
 * string utils tool class
 * </pre>
 *
 * @author TaiJL
 */
public class StringUtils {

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }

    /**
     * <p/>
     * to judge whether an object is empty <br>
     * 2014-10-23 2:41:59
     *
     * @param obj object to be tested
     * @return true: no empty false: empty
     */
    public static boolean isNotEmpty(Object obj) {
        return null != obj;
    }

    /**
     * <p/>
     * checks if a string is empty <br>
     * 2014-10-232:46:18
     *
     * @param element the string to be tested
     * @return true: no empty false: empty
     */
    public static boolean isNotEmpty(String element) {
        return null != element && !"".equals(element) && !"null".equals(element) && !"".equals(element.trim());
    }

    /**
     * <p/>
     * to judge whether a collection object is empty <br>
     * 2014-11-18 4:53:59
     *
     * @param list the collection object
     * @return true: no empty false: empty
     */
    public static boolean isNotEmpty(Collection<?> list) {
        return null != list && list.size() > 0;
    }

    /**
     * <p/>
     * judge a map < k, v > set whether the object is empty <br>
     * 2014-11-18 4:53:59
     *
     * @param map the collection object
     * @return true: no empty false: empty
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return null != map && map.size() > 0;
    }

    /**
     * Indicates whether the specified string is a digit
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (isEmpty(str)) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static String stringAndNumberFilter(String parameter) throws PatternSyntaxException {
        String regEx = "[^a-zA-Z0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(parameter);
        return m.replaceAll("").trim();
    }

    /**
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static String stringFilter(String parameter) throws PatternSyntaxException {
        String regEx = "[^a-zA-Z]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(parameter);
        return m.replaceAll("").trim();
    }

    /**
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static String numberFilter(String parameter) throws PatternSyntaxException {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(parameter);
        return m.replaceAll("").trim();
    }

    /**
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static boolean payPasswordFilter(String parameter) throws PatternSyntaxException {
        String regEx = "^[1-9]{1}[0-9]{5}$";
        boolean result = Pattern.matches(regEx, parameter);
        return result;
    }

    /**
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static boolean phoneFilter(String parameter) throws PatternSyntaxException {
        if (null == parameter) {
            return false;
        }
        String regEx = "^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$";
        boolean result = Pattern.matches(regEx, parameter);
        return result;
    }

    /**
     * 检测密码长度{6,20},且同时需包含数字和字符
     *
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static boolean passwordFilter(String parameter) throws PatternSyntaxException {
        if (null == parameter) {
            return false;
        }
        String regEx = "^[0-9]{1,20}$";
        String regStr = "(?=.*?[0-9])[a-zA-Z0-9]{6,20}$";
        boolean result = Pattern.matches(regEx, parameter);
        boolean resultStr = Pattern.matches(regStr, parameter);
        if (result) {
            return false;
        }
        return resultStr;
    }


    /**
     * 检测密码长度{6,18}
     *
     * @param parameter 参数
     * @return 格式化后的参数
     * @throws PatternSyntaxException
     */
    public static boolean passwordLengthFilter(String parameter) throws PatternSyntaxException {
        if (null == parameter) {
            return false;
        }
        return parameter.length() >= 6 && parameter.length() <= 18;
    }

    /**
     * 校验邮箱是否合法
     *
     * @param mail 待校验的邮箱地址
     * @return
     */
    public static boolean isMail(String mail) {
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(mail);
        return matcher.matches();
    }

    /**
     * url中有中文字符转码
     *
     * @param str 原字符
     * @return 转码之后的url
     */
    public static String utf8Togb2312(String str) {
        String data = "";
        try {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c + "".getBytes().length > 1 && c != ':' && c != '/') {
                    data = data + java.net.URLEncoder.encode(c + "", "utf-8");
                } else {
                    data = data + c;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
        }
        return data;
    }

    /**
     * 隐藏手机号中间四位
     *
     * @param mobiles
     * @return
     */
    public static String hideMobileNO(String mobiles) {
        if (!TextUtils.isEmpty(mobiles) && mobiles.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mobiles.length(); i++) {
                char c = mobiles.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        } else {
            return null;
        }
    }

    /**
     * 获取带两位小数的分数，如果为100则显示整数
     *
     * @param score
     * @return
     */
    public static String getTwoDecimalScore(String score) {
        String decimalScore = "0.00";
        if (!TextUtils.isEmpty(score)) {
            double scoreDouble = Double.parseDouble(score);
            decimalScore = getTwoDecimalScore(scoreDouble);
        }
        return decimalScore;
    }

    /**
     * 获取带两位小数的分数，如果为100则显示整数
     *
     * @param score
     * @return
     */
    public static String getTwoDecimalScore(double score) {
        String decimalScore = "";
        int scoreInt = (int) score;
        if (scoreInt == 100) {
            decimalScore = "100";
        } else {
            DecimalFormat df = new DecimalFormat("######0.00");
            decimalScore = df.format(score);
        }
        return decimalScore;
    }

    /**
     * URL字符转义
     * 　　用其它字符替代吧，或用全角的。
     * 　　+    URL 中+号表示空格                                 %2B
     * 空格 URL中的空格可以用+号或者编码           %20
     * /   分隔目录和子目录                                     %2F
     * ?    分隔实际的URL和参数                             %3F
     * %    指定特殊字符                                          %25
     * #    表示书签                                                  %23
     * &    URL 中指定的参数间的分隔符                  %26
     * =    URL 中指定参数的值                                %3D
     *
     * @param url
     * @return
     */
    public static String replaceUrlSpecialCode(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        return url.replace("%", "%25")
                .replace("+", "%2B")
                .replace(" ", "%20")
                .replace("/", "%2F")
                .replace("?", "%3F")
                .replace("#", "%23")
                .replace("&", "%26")
                .replace("=", "%3D");
    }
}